package com.game.common.utils;

import com.game.common.core.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.concurrent.TimeUnit;

/**
 * @author: zhaoyongke
 * @create: 2025-03-21 10:07
 * @description: 验证码
 */

@Component
public class YzCodeUtils {


    private  static  String  key = "captcha_codesapp";

    @Autowired
    private RedisCache redisCache;
    @Autowired
    private  SmsUtils smsUtils;
    public     String checkRedisKey( String phone,String verificationCode) {
        String msg = "";
        if (!redisCache.hasKey(key+phone)) {
            msg = "验证码已失效,请重新获取";
        }

        String code = redisCache.getCacheObject(key+phone);
        if (!verificationCode.equals(code)) {
            msg = "手机验证码不正确";
        }
        return msg;
    }

    public   Boolean   sendPhoneCode(@RequestParam(name = "phone", required = true) String phone) {
        // 验证码
        String randomString = InvitationCodeGnerateUtil.getSecurityCode();
        //将验证码存到redis中 以手机号为Key 随机字符串为value 默认5分钟
        redisCache.setCacheObject(key + phone, randomString, 300, TimeUnit.SECONDS);
        boolean b = smsUtils.sendXSms(phone, randomString);
        return b;
    }


}
